define([
    "underscore",
    "backbone",
    "i18n!js/nls/messages",
    "text!html/global/popup.html"
], function (_, Backbone, i18n, popupTemp) {
    'use strict';

    var common = {
        doc: window.document,
        addClass: function (target, className) {
            var select = $(target);
            common.removeClass(target, className);
            select.addClass(className);
        },
        removeClass: function (target, className) {
            var select = $(target);
            if (select.hasClass(className)) {
                select.removeClass(className);
            }
        },
        setStyle: function (style) {
            $("#headStyle").text(resetCss(style));
        },
        setBackground: function (bg) {
            bg = "resources/image_web/bg/" + (bg || getRandom(1, 24)) + ".jpg";
            $("#mainContent").css('background-image', "url(/" + bg.getPathMd5() + ")");
        },
        goto: function (url, reload) {
            url = url || "";
            window.location.href = "#" + url;
            // if (reload || true)
            //     window.location.reload();
        },
    };

    Backbone.View.extend = function (base) {
        return function (obj) {
            var view = $.extend({}, {
                id: "page",
                tagName: "div",
                className: "content",
                mainId: "#mainContent",
                events: {},
                initialize: function (options) {
                    var me = this;
                    this.model = options || {};
                    if (options) {
                        this.options = options;
                        this.cmd = options.cmd || {};
                        this.ajax = options.ajax || {};
                        this.link = options.link || {};
                        this.model = options.json || {};
                        console.log("[view] " + options.url, this.model);
                        common.setStyle(options.css);
                        common.setBackground(options.bg);
                        this.createDom(this.options.html);
                        this.isAnimationing = true;
                        $(this.mainId).fadeIn(400, function () {
                            common.ready.hide();
                            me.isAnimationing = false;
                            if (me.start) {
                                me.start();
                            }
                        });
                    } else {
                        this.createDom();
                    }
                },
                createDom: function (tpl) {
                    this.remove(true);
                    this.tpl = _.template(tpl);
                    $(this.mainId).prepend(this.render().el);
                },
                render: function () {
                    this.$el.html(this.tpl({
                        model: this.getModel ? this.getModel() : this.model,
                        lan: i18n
                    }));
                    return this;
                },
                remove: function (hide) {
                    this.clearUp();
                    if (hide) $(this.mainId).hide();
                    $(this.mainId + " ." + this.className).remove();
                },
                intervalArray: [],
                timeoutArray: [],
                clearUp: function () {
                    //this.off();
                    for (var i in this.intervalArray) {
                        clearInterval(this.intervalArray[i]);
                    }
                    for (var i in this.timeoutArray) {
                        clearTimeout(this.timeoutArray[i]);
                    }
                    this.intervalArray = [];
                    this.timeoutArray = [];
                },
                goto: common.goto,
            }, obj);
            //点击事件统一拦截
            view.events = function () {
                var evtObj = $.isFunction(view.events) ? view.events() : view.events;
                for (var name in evtObj) {
                    var val = evtObj[name];
                    evtObj[name] = function (e) {
                        if (this.isAnimationing) return;
                        // //阻止浏览器的默认行为//如果提供了事件对象，则这是一个非IE浏览器
                        // if (e && e.preventDefault) {
                        //     e.preventDefault();//阻止默认浏览器动作(W3C)
                        // } else {
                        //     window.event.returnValue = false;//IE中阻止函数器默认动作的方式
                        // }
                        // //停止事件冒泡//如果提供了事件对象，则这是一个非IE浏览器
                        // if (e && e.stopPropagation) {
                        //     e.stopPropagation();//因此它支持W3C的stopPropagation()方法
                        // } else {
                        //     window.event.cancelBubble = true;//否则，我们需要使用IE的方式来取消事件冒泡
                        // }
                        if (!$.isFunction(val) && (val in view)) {
                            val = view[val];
                        }
                        val.apply(this);//纠正this指向
                    };
                }
                return evtObj;
            }();
            //console.log("reset Backbone.View.extend");
            return base.call(this, view);
        }
    }(Backbone.View.extend);

    common.popup = {
        hide: function () {
            $("#mainPopup").hide();
        },
        show: function (options) {
            var PopupView = Backbone.View.extend({
                id: "popup",
                className: "content",
                mainId: "#mainPopup",
                initialize: function (options) {
                    this.model = $.extend({
                        title: "标题",
                        type: "A",
                        content: "回首頁",
                        yes: "回首頁",
                        canClose: false,
                        callback: function () {

                        },
                    }, options);
                    this.type = "#popup" + this.model.type;
                    this.createDom($(this.type).html());
                },
                events: function () {
                    var evtObj = {};
                    evtObj["click #contentline .line.yes"] = this.yesClick;
                    evtObj["click #contentline .line.not"] = this.notClick;
                    return evtObj;
                },
                yesClick: function () {
                    this.remove(true);
                    this.model.callback(true);
                },
                notClick: function () {
                    this.remove(true);
                    this.model.callback(false);
                },
                getModel: function () {
                    return {
                        title: this.model.title || "标题",
                        content: this.model.content,
                        yes: this.model.yes || i18n.confirm || "确定",
                        not: this.model.not || i18n.cancel || "关闭",
                    };
                },
                createDom: function (tpl) {
                    this.remove();
                    this.tpl = _.template(tpl);
                    $(this.mainId).append(this.render().el);
                    $(this.mainId + " .mask").css("opacity", 0.3);
                    $(this.mainId).show();
                }
            });
            new PopupView(options);
        }
    };
    common.guide = $("#guide");
    common.ready = $("#ready");
    common.log = $("#log");

    common.init = function () {
        // common.log.hide();
        common.ready.hide();
        common.popup.hide();
        common.guide.hide();
        $("<div></div>").html(popupTemp).prependTo($("body"));
    };

    return common;
});